package com.aegean.MonitorExamSYS.monitor.action;

import java.io.File;
import java.io.FileInputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.examinee.business.ExamineeBusiness;
import com.aegean.MonitorExamSYS.monitor.business.MonitorBusiness;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.aegean.MonitorExaxSYS.model.component.Photo;
import com.opensymphony.xwork2.ActionSupport;
@Controller("MonitorRegister")
@Scope("prototype")
public class MonitorRegister extends ActionSupport{
	@Resource
	MonitorBusiness business;
	private MonitorValue monitor;
	private String userRPwd;
	private int big_Id;
	private int unit_Id;
	private File photo;
	private String photoFileName;
	private String photoContentType;
	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public int getBig_Id() {
		return big_Id;
	}

	public void setBig_Id(int big_Id) {
		this.big_Id = big_Id;
	}

	public int getUnit_Id() {
		return unit_Id;
	}

	public void setUnit_Id(int unit_Id) {
		this.unit_Id = unit_Id;
	}

	public String getUserRPwd() {
		return userRPwd;
	}

	public void setUserRPwd(String userRPwd) {
		this.userRPwd = userRPwd;
	}

	private int conduction_Id;
	public int getConduction_Id() {
		return conduction_Id;
	}

	public void setConduction_Id(int conduction_Id) {
		this.conduction_Id = conduction_Id;
	}

	public MonitorValue getMonitor() {
		return monitor;
	}

	public void setMonitor(MonitorValue monitor) {
		this.monitor = monitor;
	}
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		if (photo == null || photo.length() == 0) {
			this.addFieldError("photo", "对不起，该头像不能为空");
		}
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		try {
			Photo sphoto = new Photo();
			sphoto.setPhoto(Hibernate.createBlob(new FileInputStream(photo)));
			sphoto.setPhoto_Name(photoFileName);
			sphoto.setPhoto_Type(photoContentType);
			monitor.setUser_Photo(sphoto);
			monitor=business.insertMonitor(monitor, big_Id, unit_Id);
			if(monitor!=null){
				return SUCCESS;
			} else{
				return ERROR;
			}
		}catch(Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}
	
}
